﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace LibraryCS
{
    public class Testing : VisionAlgorithm
    {
        VImage vi;
        public Testing(String name)
            : base(name)
        {
        }

        public byte[] contouring(byte[] binaryImage)
        {
            byte[] returnImage = (byte[])binaryImage.Clone();
            for (int y = 0; y < vi.Height; y++)
            {
                for (int x = 0; x < vi.Width; x++)
                {
                    int location = y * vi.Width + x;
                    if (binaryImage[location] == 0)
                    {
                        bool filling = true;
                        for (int q = -1; q < 2; q++)
                        {
                            for (int p = -1; p < 2; p++)
                            {
                                if (x + p < 0 || y + q < 0 || x + p > vi.Width - 1 || y + q > vi.Height - 1)
                                {
                                    filling = false;
                                    continue;
                                }
                                if (binaryImage[location + (q * vi.Width )+ p] == 255)
                                {
                                    filling = false;
                                }
                            }
                        }
                        if (filling)
                        {
                            returnImage[location] = 255;
                        }
                    }
                }
            }
            return returnImage;
        }

        public override System.Drawing.Bitmap DoAlgorithm(System.Drawing.Bitmap sourceImage)
        {
            vi = new VImage(sourceImage);
            VImage vioutput = new VImage(vi.Width, vi.Height);
            byte[] output = contouring(vi.GrayPixels);

            return vioutput.returnNewBitmapOutput(output);
        }
    }
}
